#!/bin/bash

source /home/jhipster/.profile

cd /home/jhipster

# install jdk
which java >/dev/null 2>&1
if [ $? -ne 0 ];then
    wget -q http://kaifa.hc-yun.com:30027/workspace/soft/jdk-8u171-linux-x64.tar.gz
    tar -zxf jdk-8u171-linux-x64.tar.gz -C /home/jhipster
    ln -sf /home/jhipster/jdk1.8.0_171 /home/jhipster/java

    echo -e '\n# add env jdk'  >> /home/jhipster/.profile 
    echo 'JAVA_HOME=/home/jhipster/java' >> /home/jhipster/.profile
    echo 'JRE_HOME=${JAVA_HOME}/jre' >> /home/jhipster/.profile
    echo 'CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib' >> /home/jhipster/.profile
    echo 'PATH=$JAVA_HOME/bin:$PATH' >> /home/jhipster/.profile
    echo 'export PATH JAVA_HOME CLASSPATH' >> /home/jhipster/.profile
fi


# install maven
which mvn >/dev/null 2>&1
if [ $? -ne 0 ];then
    wget -q http://apache.communilink.net/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
    tar -zxf  apache-maven-3.5.4-bin.tar.gz -C /home/jhipster
    ln -sf /home/jhipster/apache-maven-3.5.4 /home/jhipster/maven

    # config maven repo
    line=$(grep -rn '<mirrors>' /home/jhipster/maven/conf/settings.xml | awk -F : '{print $1}')
    sed -i "${line}a\    </mirror>" /home/jhipster/maven/conf/settings.xml
    sed -i "${line}a\        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>" /home/jhipster/maven/conf/settings.xml
    sed -i "${line}a\        <mirrorOf>*,\!maven_nexus_201</mirrorOf>" /home/jhipster/maven/conf/settings.xml 
    sed -i "${line}a\        <id>aliyun_nexus</id>" /home/jhipster/maven/conf/settings.xml
    sed -i "${line}a\    <mirror>" /home/jhipster/maven/conf/settings.xml
    line=$(grep -rn '<pluginGroups>' /home/jhipster/maven/conf/settings.xml | awk -F : '{print $1}')
    sed -i "${line}a\      <pluginGroup>com.spotify</pluginGroup>" /home/jhipster/maven/conf/settings.xml

    echo -e '\n# add mvn jdk'  >> /home/jhipster/.profile 
    echo 'export MAVEN_HOME=/home/jhipster/maven' >> /home/jhipster/.profile
    echo 'export PATH=$PATH:$MAVEN_HOME/bin' >> /home/jhipster/.profile
fi